from django.http import HttpResponse, JsonResponse
from django.shortcuts import render

from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt, csrf_protect

# Create your views here.

# @csrf_exempt #为某个视图函数免除csrf认证
# @csrf_protect #为某个视图函数单独添加csrf认证
from rest_framework.views import APIView

from app1.models import User


@csrf_exempt
def user(request):
    if request.method == "GET":
        username = request.GET.get("username")
        print(username)
        print("GET 查询")
        return HttpResponse("GET OK")
    if request.method == "POST":
        print("POST 新增")
        return HttpResponse("POST OK")
    if request.method == "PUT":
        print("PUT 修改")
        return HttpResponse("PUT OK")
    if request.method == "DELETE":
        print("DELETE 删除")
        return HttpResponse("DELETE OK")


# 为类视图添加csrf认证
# @method_decorator(csrf_protect,name='dispatch')
@method_decorator(csrf_exempt,name='dispatch')
class UserView(View):
    def get(self, request, *args, **kwargs):
        """
        提供查询单个用户以及多个用户的接口
        :param request: 请求对象
        :param args:
        :param kwargs:
        :return: 返回查询结果
        """
        user_id = kwargs.get("id")
        print(user_id)

        if user_id:
            user_val = User.objects.filter(pk=user_id).values("username", "password", "gender").first()
            print(user)
            if user:
                return JsonResponse({
                    "status": 200,
                    "message": "查询单个用户成功",
                    "results": user_val,
                })
        else:
            # 用户id不存在时,返回所有
            user_objects_all = User.objects.all().values("username","password","gender")
            if user_objects_all:
                return  JsonResponse({
                    "status":200,
                    "message":"查询所有用户成功",
                    "results": list(user_objects_all),
                })
            return JsonResponse({
                "status":400,
                "message":"查询失败",
            })

    def post(self,request,*args,**kwargs):
        """
        新增单个用户
        """
        username = request.POST.get("username")
        pwd = request.POST.get("password")
        try:
            user_obj = User.objects.create(username=username,password =pwd)
            return JsonResponse({
                "status":200,
                "message":"新增单个用户成功",
                "results": {"username":user_obj.username}
            })
        except:
            return JsonResponse({
                "status": 400,
                "message":"新增失败"
            })

    def put(self,request,*args,**kwargs):
        print("put修改")
        return HttpResponse()
    def delete(self,request,*args,**kwargs):
        print("delete 删除")
        return JsonResponse({
            "status":200,
            "message":"删除成功"
        })

class StudentAPIView(APIView):
    def get(self,request,*args,**kwargs):
        return JsonResponse({
            "status":200,
            "message":"get"
        })
    def post(self,request,*args,**kwargs):
        return JsonResponse({

        })